package com.yanqu.road.server.gameplayer.module.activity.zhenhuan;

import com.yanqu.road.dao.impl.activity.zhenhuan.UserZhenHuanPartyHistoryDaoImpl;
import com.yanqu.road.dao.impl.player.UserZhenHuanGoingOutDaoImpl;
import com.yanqu.road.dao.impl.player.UserZhenHuanKickDaoImpl;
import com.yanqu.road.dao.impl.player.UserZhenHuanSecondDaoImpl;
import com.yanqu.road.dao.impl.player.UserZhenHuanSecondRelationDaoImpl;
import com.yanqu.road.entity.activity.zhenhuan.config.ZhenHuanLevelConfig;
import com.yanqu.road.entity.activity.zhenhuan.secondProcess.*;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.entity.enums.eSystemId;
import com.yanqu.road.entity.log.LogZhenHuanSecondGoingOut;
import com.yanqu.road.entity.log.LogZhenHuanSecondJoinParty;
import com.yanqu.road.entity.log.LogZhenHuanSecondOpenParty;
import com.yanqu.road.entity.player.PlayerState;
import com.yanqu.road.entity.relatives.RelativesInfo;
import com.yanqu.road.logic.activity.zhenhuan.ZhenHuanGoingOutResult;
import com.yanqu.road.logic.activity.zhenhuan.ZhenHuanPb;
import com.yanqu.road.logic.activity.zhenhuan.ZhenHuanSecondProcessConfig;
import com.yanqu.road.logic.helper.CalcPowerResult;
import com.yanqu.road.logic.helper.PowerHelper;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.pb.activity.ZhenHuanProto;
import com.yanqu.road.pb.player.PlayerProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.ActivityWorshipModule;
import com.yanqu.road.server.gameplayer.module.base.GeneralModule;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.gameplayer.module.player.PreReduceModule;
import com.yanqu.road.server.gameplayer.module.relatives.RelativesModule;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.zhenhuan.ZhenHuanConfigMgr;
import com.yanqu.road.server.manger.activity.zhenhuan.ZhenHuanMgr;
import com.yanqu.road.server.manger.config.SystemOpenMgr;
import com.yanqu.road.server.manger.log.AutoLogMgr;
import com.yanqu.road.server.manger.maidian.zhenhuan.ZhenHuanLog;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.server.manger.union.CrossUnionMgr;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.utils.date.DateHelper;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class ZhenHuanSecondProcessModule extends GeneralModule {
    public ZhenHuanSecondProcessModule(GamePlayer player) {
        super(player);
    }

    private UserZhenHuanSecondProcessData userData;
    private Map<Integer, UserZhenHuanGoingOutData> goingOutDataMap = new ConcurrentHashMap<>();

    private List<UserZhenHuanKickData> kickDataList = new ArrayList<>();
    private Map<Long, UserZhenHuanPartyHistoryData> partyHistoryDataMap = new ConcurrentHashMap<>();

    //我参加过的宴会玩家名单 key targetUserId
    private Map<Long, UserZhenHuanSecondRelationData> joinUserMap = new ConcurrentHashMap<>();

    @Override
    public boolean loadData() {
        ZhenHuanSecondProcessConfig secondProcessConfig = ZhenHuanConfigMgr.getSecondProcessConfig();
        if (secondProcessConfig == null) {
            return true;
        }
        userData = new UserZhenHuanSecondDaoImpl().getUserZhenHuanSecondProcessData(secondProcessConfig.getActivityId(), player.getUserId());
        goingOutDataMap = new UserZhenHuanGoingOutDaoImpl().getUserZhenHuanGoingOutData(secondProcessConfig.getActivityId(), getUserId());
        kickDataList = new UserZhenHuanKickDaoImpl().getUserZhenHuanKickData(secondProcessConfig.getActivityId(), getUserId());
        partyHistoryDataMap = new UserZhenHuanPartyHistoryDaoImpl().getUserZhenHuanPartyHistoryData(secondProcessConfig.getActivityId(), getUserId());
        joinUserMap = new UserZhenHuanSecondRelationDaoImpl().getUserZhenHuanSecondRelationData(secondProcessConfig.getActivityId(), getUserId());

        removeOverNumHistoryParty();

        return true;
    }

    @Override
    public boolean afterLoadData() {
        return true;
    }

    @Override
    public boolean saveData() {
        if (userData != null) {
            if (userData.isInsertOption()) {
                new UserZhenHuanSecondDaoImpl().add(userData);
            } else if (userData.isUpdateOption()) {
                new UserZhenHuanSecondDaoImpl().update(userData);
            }
        }

        List<UserZhenHuanGoingOutData> saveGoingOutDataList = new ArrayList<>();
        synchronized (goingOutDataMap) {
            saveGoingOutDataList.addAll(goingOutDataMap.values());
        }

        for (UserZhenHuanGoingOutData goingOutData : saveGoingOutDataList) {
            if (goingOutData.isInsertOption()) {
                new UserZhenHuanGoingOutDaoImpl().add(goingOutData);
            } else if (goingOutData.isUpdateOption()) {
                new UserZhenHuanGoingOutDaoImpl().update(goingOutData);
            }
        }

        List<UserZhenHuanKickData> saveKickDataList = new ArrayList<>();
        synchronized (kickDataList) {
            saveKickDataList.addAll(kickDataList);
        }

        for (UserZhenHuanKickData kickData : saveKickDataList) {
            if (kickData.isInsertOption()) {
                new UserZhenHuanKickDaoImpl().add(kickData);
            } else if (kickData.isUpdateOption()) {
                new UserZhenHuanKickDaoImpl().update(kickData);
            }
        }

        List<UserZhenHuanPartyHistoryData> savePartyHistoryDataList = new ArrayList<>();
        synchronized (partyHistoryDataMap) {
            savePartyHistoryDataList.addAll(partyHistoryDataMap.values());
        }
        for (UserZhenHuanPartyHistoryData historyData : savePartyHistoryDataList) {
            if (historyData.isInsertOption()) {
                new UserZhenHuanPartyHistoryDaoImpl().add(historyData);
            } else if (historyData.isUpdateOption()) {
                new UserZhenHuanPartyHistoryDaoImpl().update(historyData);
            }
        }

        List<UserZhenHuanSecondRelationData> saveJoinUserList = new ArrayList<>();
        synchronized (joinUserMap) {
            saveJoinUserList.addAll(joinUserMap.values());
        }
        for (UserZhenHuanSecondRelationData relationData : saveJoinUserList) {
            if (relationData.isInsertOption()) {
                new UserZhenHuanSecondRelationDaoImpl().add(relationData);
            } else if (relationData.isUpdateOption()) {
                new UserZhenHuanSecondRelationDaoImpl().update(relationData);
            }
        }

        return true;
    }

    @Override
    public void afterLogin() {
        initUserData();
        syncConfig();
        syncUserData();

//        if (userData != null) {
//            player.notifyListener(eGamePlayerEventType.ZhenHuanSecondProcessRank.getValue());
//        }
    }

    public void syncUserData() {
        if (userData == null) {
            return;
        }

        userData.setAble(ZhenHuanMgr.isActivityAble(getUserId(), userData.getActivityId()));

        int nowPower = getNowPower();
        ZhenHuanProto.ZhenHuanSecondProcessUserSyncMsg.Builder syncMsg = ZhenHuanPb.parseSecondProcessUserSyncMsg(userData);
        syncMsg.setIsWorship(player.getModule(ActivityWorshipModule.class).isWorship(userData.getActivityId()));
        player.sendPacket(ClientProtocol.U_ZHENHUAN_SECOND_PROCESS_USER_SYNC, syncMsg);
    }

    private void syncConfig() {
        if (userData == null) {
            return;
        }
        ZhenHuanSecondProcessConfig config = ZhenHuanConfigMgr.getSecondProcessConfig();
        if (config == null) {
            return;
        }
        ZhenHuanProto.ZhenHuanSecondProcessConfigSyncMsg configMsg = ZhenHuanConfigMgr.getSecondProcessConfigMsg(player.getLanguage());
        player.sendPacket(ClientProtocol.U_ZHENHUAN_SECOND_PROCESS_CONFIG_SYNC, configMsg.toBuilder());

    }

    private void initUserData() {
        if (!SystemOpenMgr.systemOpen(player, eSystemId.ZhenHuanSecondProcess.getValue())) {
            return;
        }
        ZhenHuanSecondProcessConfig config = ZhenHuanConfigMgr.getSecondProcessConfig();
        if (config == null) {
            return;
        }
        if (userData == null || userData.getActivityId() != config.getActivityId()) {
            userData = new UserZhenHuanSecondProcessData(config.getActivityId(), player.getUserId());
            goingOutDataMap = new ConcurrentHashMap<>();
        }

    }

    public void iniSystem() {
        if (!SystemOpenMgr.systemOpen(player, eSystemId.ZhenHuanSecondProcess.getValue())) {
            return;
        }
        initUserData();
        syncConfig();
        syncUserData();
    }

    public long calcNextPowerTime() {
        if (userData == null) {
            return -1;
        }
        ZhenHuanSecondProcessConfig config = ZhenHuanConfigMgr.getSecondProcessConfig();
        if (config == null) {
            return -1;
        }

        if (userData.getPower() >= config.getInitPower()) {
            return 0;
        }

        long powerRecoverCD = config.getPowerRecoverCD(); // 恢复一点体力的时间秒
        long powerRecoverTime = userData.getPowerRecoverTime() / 1000; // 上次恢复体力的时间 秒
        CalcPowerResult calcPowerResult = PowerHelper.calcPower(powerRecoverTime, powerRecoverCD, 1, config.getInitPower(), userData.getPower());
        userData.setPower(calcPowerResult.getCurPower());
        userData.setPowerRecoverTime(calcPowerResult.getLastRecoverTime() * 1000);

        return userData.getPowerRecoverTime() + powerRecoverCD * 1000;
    }

    public int getNowPower() {
        long ret = calcNextPowerTime();
        if (ret == -1) {
            return 0;
        }
        return userData.getPower();
    }

    public void addPower(int power) {
        long ret = calcNextPowerTime();
        if (ret == -1) {
            return;
        }
        userData.setPower(userData.getPower() + power);
        syncUserData();
    }

    public void usePower(int power) {
        long ret = calcNextPowerTime();
        if (ret == -1) {
            return;
        }
        ZhenHuanSecondProcessConfig config = ZhenHuanConfigMgr.getSecondProcessConfig();
        int maxPower = config.getInitPower();
        int oldPower = userData.getPower();
        userData.setPower(userData.getPower() - power);
        if (oldPower >= maxPower && userData.getPower() < maxPower) {
            userData.setPowerRecoverTime(DateHelper.getCurrentTime());
        }
    }

    public int checkAction(int checkActivityId) {
        if (userData == null) {
            return GameErrorCode.E_SYSTEM_NO_OPEN;
        }

        ZhenHuanSecondProcessConfig config = ZhenHuanConfigMgr.getSecondProcessConfig();
        if (config == null) {
            return GameErrorCode.E_ACTIVITY_NOT_OPEN;
        }

        if (!ActivityMgr.activityInTime(config.getActivityInfo())) {
            return GameErrorCode.E_ACTIVITY_NOT_OPEN;
        }

        if (!userData.isAble()) {
            return GameErrorCode.E_ZHENHUAN_SECOND_PROCESS_NOT_ABLE;
        }

        if (config.getActivityId() != checkActivityId) {
            return GameErrorCode.E_ACTIVITY_NO_FOUND;
        }
        return 0;
    }

    public int checkShowTimeAction(int activityId) {
        if (userData == null) {
            return GameErrorCode.E_SYSTEM_NO_OPEN;
        }

        ZhenHuanSecondProcessConfig config = ZhenHuanConfigMgr.getSecondProcessConfig();
        if (config == null) {
            return GameErrorCode.E_ACTIVITY_NOT_OPEN;
        }

        if (!ActivityMgr.activityInShowTime(config.getActivityInfo())) {
            return GameErrorCode.E_ACTIVITY_NOT_OPEN;
        }

        if (config.getActivityId() != activityId) {
            return GameErrorCode.E_ACTIVITY_NO_FOUND;
        }
        return 0;
    }

    public int getGoingOutMainScene(int activityId) {
        int ret = checkAction(activityId);
        if (ret != 0) {
            return ret;
        }

        ZhenHuanSecondProcessConfig config = ZhenHuanConfigMgr.getSecondProcessConfig();

        ZhenHuanProto.ZhenHuanSecondProcessGoingOutMainSceneRespMsg.Builder respMsg = ZhenHuanProto.ZhenHuanSecondProcessGoingOutMainSceneRespMsg.newBuilder();
        respMsg.setRet(0);
        respMsg.setActivityId(config.getActivityId());
        respMsg.setLevel(userData.getLevel());
        respMsg.setLoveDegree(userData.getLoveDegree());
        respMsg.setPrestigeDegree(userData.getPrestigeDegree());
        respMsg.setPower(getNowPower());
        respMsg.setNextPowerTime(calcNextPowerTime());
        respMsg.addAllPlace(ZhenHuanPb.parsePlaceTempList(new ArrayList<>(goingOutDataMap.values())));
        respMsg.setGoingOutNum(userData.getGoingOutAmount());
        player.sendPacket(ClientProtocol.U_ZHENHUAN_SECOND_PROCESS_GOING_OUT_MAIN_SCENE, respMsg);

        return 0;
    }

    public int goingOut(int activityId, int num) {
        int ret = checkAction(activityId);
        if (ret != 0) {
            return ret;
        }

        if (num < 1 || num > 10) {
            return GameErrorCode.E_ERROR_DATA;
        }

        ZhenHuanSecondProcessConfig config = ZhenHuanConfigMgr.getSecondProcessConfig();
        if (num > 1) {
            if (userData.getGoingOutAmount() < config.getMultiGoingOutLimit()){
                return GameErrorCode.E_ZHENHUAN_SECOND_PROCESS_GOING_OUT_NUM_LIMIT;
            }
        }

        int nowPower = getNowPower();
        if (nowPower < config.getPowerCostNum()) {//少于一次的消耗
            return GameErrorCode.E_ZHENHUAN_SECOND_PROCESS_POWER_NOT_ENOUGH;
        }

        if (nowPower < config.getPowerCostNum() * num) {//能用几次算几次
            num = nowPower / config.getPowerCostNum();
        }

        if (num < 1) {
            return GameErrorCode.E_ZHENHUAN_SECOND_PROCESS_POWER_NOT_ENOUGH;
        }

        ZhenHuanGoingOutResult goingOutResult = config.goingOut(num, userData.getGoingOutAmount(), goingOutDataMap);

        usePower(config.getPowerCostNum() * num);

        userData.setGoingOutAmount(goingOutResult.getGoingOutAmount());
        userData.setLoveDegree(userData.getLoveDegree() + goingOutResult.getTotalAddLoveDegree());
        for (Map.Entry<Integer, Integer> entry : goingOutResult.getNewProgressMap().entrySet()) {
            Integer placeId = entry.getKey();
            UserZhenHuanGoingOutData goingOutData = getGoingOutDataNewIfNull(config.getActivityId(), placeId);
            goingOutData.setProgress(entry.getValue());
        }

        for (Map.Entry<Integer, Integer> entry : goingOutResult.getNewAmountMap().entrySet()) {
            Integer placeId = entry.getKey();
            UserZhenHuanGoingOutData goingOutData = getGoingOutDataNewIfNull(config.getActivityId(), placeId);
            goingOutData.setAmount(entry.getValue());
        }

        player.getModule(CurrencyModule.class).addCurrency(goingOutResult.getTotalReward(),
                eLogMoneyType.ZhenHuanSecondProcess, eLogMoneyType.ZhenHuanSecondProcessGoingOutReward);

        ZhenHuanProto.ZhenHuanSecondProcessGoingOutRespMsg.Builder respMsg = ZhenHuanProto.ZhenHuanSecondProcessGoingOutRespMsg.newBuilder();
        respMsg.setRet(0);
        respMsg.addAllResult(goingOutResult.getResultMsgList());
        player.sendPacket(ClientProtocol.U_ZHENHUAN_SECOND_PROCESS_GOING_OUT, respMsg);

        syncUserData();

        player.notifyListener(eGamePlayerEventType.ZhenHuanSecondProcessLoveDegree.getValue());

        AutoLogMgr.add(new LogZhenHuanSecondGoingOut(activityId, getUserId(), nowPower, config.getPowerCostNum() * num, goingOutResult.getTotalAddLoveDegree(),
                goingOutResult.getPlaceAddMap(), goingOutResult.getPlaceFullMap(), goingOutResult.getPlaceReward(), goingOutResult.getFullReward()));

        player.notifyListener(eGamePlayerEventType.ZhenHuanSecondProcessRank.getValue());

        return 0;
    }

    private UserZhenHuanGoingOutData getGoingOutDataNewIfNull(int activityId, int placeId) {
        UserZhenHuanGoingOutData userZhenHuanGoingOutData = goingOutDataMap.get(placeId);
        if (userZhenHuanGoingOutData == null) {
            synchronized (goingOutDataMap) {
                userZhenHuanGoingOutData = goingOutDataMap.get(placeId);
                if (userZhenHuanGoingOutData == null) {
                    userZhenHuanGoingOutData = new UserZhenHuanGoingOutData(activityId, getUserId(), placeId);
                    goingOutDataMap.put(placeId, userZhenHuanGoingOutData);
                }
            }
        }
        return userZhenHuanGoingOutData;
    }

    public int levelUp(int activityId) {
        int ret = checkAction(activityId);
        if (ret != 0) {
            return ret;
        }

        ZhenHuanSecondProcessConfig config = ZhenHuanConfigMgr.getSecondProcessConfig();

        ZhenHuanLevelConfig nextLevelConfig = config.getLevelConfig(userData.getLevel() + 1);
        if (nextLevelConfig == null) {
            return GameErrorCode.E_ZHENHUAN_SECOND_PROCESS_LEVEL_MAX;
        }

        if (userData.getLoveDegree() < nextLevelConfig.getLoveDegree()) {
            return GameErrorCode.E_ZHENHUAN_SECOND_PROCESS_LOVE_DEGREE_NOT_ENOUGH;
        }

        if (userData.getPrestigeDegree() < nextLevelConfig.getPrestigeDegree()) {
            return GameErrorCode.E_ZHENHUAN_SECOND_PROCESS_PRESTIGE_DEGREE_NOT_ENOUGH;
        }

        userData.setLevel(userData.getLevel() + 1);

        Property reward = PropertyHelper.randomPropertyByListList(nextLevelConfig.getRewardList());
        player.getModule(CurrencyModule.class).addCurrency(reward, eLogMoneyType.ZhenHuanSecondProcess, eLogMoneyType.ZhenHuanSecondProcessLevelUpReward);


        ZhenHuanProto.ZhenHuanSecondProcessLevelUpRespMsg.Builder respMsg = ZhenHuanProto.ZhenHuanSecondProcessLevelUpRespMsg.newBuilder();
        respMsg.setRet(0);
        respMsg.setLevel(userData.getLevel());
        respMsg.setReward(reward.toHelperString());
        player.sendPacket(ClientProtocol.U_ZHENHUAN_SECOND_PROCESS_LEVEL_UP, respMsg);

        player.notifyListener(eGamePlayerEventType.ZhenHuanSecondProcessRank.getValue());

        syncToCross();
        return 0;
    }

    /**
     * 更新数据到活动跨服
     */
    public void syncToCross() {
        ZhenHuanSecondProcessConfig config = ZhenHuanConfigMgr.getSecondProcessConfig();
        if (config == null) {
            return;
        }
        if (userData == null) {
            return;
        }
        player.sendPacket(CrossProtocol.C_CROSS_ZHENHUAN_SECOND_PROCESS_MY_PARTY_JOIN_LEVEL_UPDATE,
                ZhenHuanProto.CrossZhenHuanSecondProcessMyPartyJoinLevelUpdateReqMsg.newBuilder().
                        setActivityId(config.getActivityId()).setLevel(userData.getLevel()).setPrestige(userData.getPrestigeDegree())
                        .setUserBaseInfo(PlayerBasePb.parsePlayerBaseTempMsg(UserMgr.getUserBaseInfo(getUserId(), GameServer.getInstance().getServerId()))));
    }

    public int getAllPartyList(ZhenHuanProto.ZhenHuanSecondProcessAllPartyListReqMsg reqMsg) {
        int ret = checkAction(reqMsg.getActivityId());
        if (ret != 0) {
            return ret;
        }

        player.sendPacket(CrossProtocol.C_CROSS_ZHENHUAN_SECOND_PROCESS_ALL_PARTY_LIST, reqMsg.toBuilder());
        return 0;
    }

    public int getPartyDetail(ZhenHuanProto.ZhenHuanSecondProcessPartyDetailReqMsg reqMsg) {
        int ret = checkAction(reqMsg.getActivityId());
        if (ret != 0) {
            return ret;
        }
        ZhenHuanSecondProcessConfig secondProcessConfig = ZhenHuanConfigMgr.getSecondProcessConfig();

        //先找下游戏服有没有，没有再去跨服拉
        GameZhenHuanFinishPartyData finishPartyData = ZhenHuanMgr.getFinishPartyData(reqMsg.getActivityId(), reqMsg.getPartyId());
        if (finishPartyData != null) {
            ZhenHuanProto.ZhenHuanSecondProcessPartyDetailRespMsg.Builder respMsg = ZhenHuanProto.ZhenHuanSecondProcessPartyDetailRespMsg.newBuilder();
            respMsg.setRet(0);
            ZhenHuanProto.ZhenHuanSecondProcessPartyDetailTemp.Builder detailTemp = ZhenHuanProto.ZhenHuanSecondProcessPartyDetailTemp.newBuilder();
            detailTemp.setPartyId(finishPartyData.getPartyId());
            detailTemp.setLevel(finishPartyData.getShowPartyLevel());
            detailTemp.setPlayerName(finishPartyData.getUserName());
            detailTemp.setDecoration(finishPartyData.getUserDecoration());
            detailTemp.setUserId(finishPartyData.getOpenUserId());
            detailTemp.setEndTime(finishPartyData.getFinishTime());
            detailTemp.setOpenServerId(finishPartyData.getOpenServerId());

            for (ZhenHuanPartyUserSimpleInfo userSimpleInfo : finishPartyData.getJoinUserList()) {
                ZhenHuanProto.ZhenHuanSecondProcessPartyPlayerTemp.Builder playerTemp = ZhenHuanProto.ZhenHuanSecondProcessPartyPlayerTemp.newBuilder();
                playerTemp.setUserId(userSimpleInfo.getUserId());
                playerTemp.setLevel(userSimpleInfo.getLevel());
                playerTemp.setJoinPrestige(userSimpleInfo.getPrestige());
                playerTemp.setPos(userSimpleInfo.getPos());
                ZhenHuanLevelConfig levelConfig = secondProcessConfig.getLevelConfig(userSimpleInfo.getLevel());
                if (levelConfig != null) {
                    playerTemp.setJoinPrestige(levelConfig.getUseInvitationPrestige());
                    playerTemp.setSharePrestige(levelConfig.getSharePrestige());
                }
                playerTemp.setFinishPrestige(userSimpleInfo.getMatchPrestige());
                playerTemp.setUserPrestige(userSimpleInfo.getPrestige());

                PlayerProto.PlayerBaseTempMsg.Builder userBaseTemp = PlayerProto.PlayerBaseTempMsg.newBuilder();
                userBaseTemp.setNickName(userSimpleInfo.getUserName());
                userBaseTemp.setServerId(userSimpleInfo.getServerId());
                userBaseTemp.setDecoration(userSimpleInfo.getUserDecoration());
                playerTemp.setUserBaseInfo(userBaseTemp);
                playerTemp.setIsNpc(secondProcessConfig.isNpc(userSimpleInfo.getUserId()));

                detailTemp.addPlayerList(playerTemp);

                if (userSimpleInfo.getPos() == 1) {//已结束的茶话会显示的是玩家获得的总威望
                    detailTemp.setExtraPrestige(userSimpleInfo.getMatchPrestige());
                }
            }
            respMsg.setDetailInfo(detailTemp);
            player.sendPacket(ClientProtocol.U_ZHENHUAN_SECOND_PROCESS_PARTY_DETAIL, respMsg);
        } else {
            player.sendPacket(CrossProtocol.C_CROSS_ZHENHUAN_SECOND_PROCESS_PARTY_DETAIL, reqMsg.toBuilder());
        }

        return 0;
    }

    public int joinParty(ZhenHuanProto.ZhenHuanSecondProcessJoinPartyReqMsg reqMsg) {
        int ret = checkAction(reqMsg.getActivityId());
        if (ret != 0) {
            return ret;
        }

        ZhenHuanSecondProcessConfig config = ZhenHuanConfigMgr.getSecondProcessConfig();
        if (!config.isInDayOptionTime()) {
            return GameErrorCode.E_ACTIVITY_NOT_OPEN;
        }

        if (reqMsg.getPos() <= 0 || reqMsg.getPos() > config.getPartyCanJoinNum() + 1) {
            return GameErrorCode.E_ERROR_DATA;
        }

        //判断材料
        Property cost = new Property(config.getJoinGoodsId(), 1);
        if (!player.getModule(CurrencyModule.class).currencyIsEnough(cost)) {
            return GameErrorCode.E_BAG_ITEM_NO_ENOUGH;
        }

        //预扣除
        player.getModule(PreReduceModule.class).preReduce(cost);

        ZhenHuanProto.CrossZhenHuanSecondProcessJoinPartyReqMsg.Builder crossReq = ZhenHuanProto.CrossZhenHuanSecondProcessJoinPartyReqMsg.newBuilder();
        crossReq.setActivityId(config.getActivityId());
        crossReq.setUserId(getUserId());
        crossReq.setUserLevel(userData.getLevel());
        crossReq.setUserPrestige(userData.getPrestigeDegree());
        crossReq.setUserBaseInfo(PlayerBasePb.parsePlayerBaseTempMsg(UserMgr.getUserBaseInfo(getUserId(), GameServer.getInstance().getServerId())));
        crossReq.setClientMsg(reqMsg);

        player.sendPacket(CrossProtocol.C_CROSS_ZHENHUAN_SECOND_PROCESS_JOIN_PARTY, crossReq);
        return 0;
    }

    public void joinPartyBack(ZhenHuanProto.ZhenHuanSecondProcessJoinPartyRespMsg respMsg) {
        ZhenHuanProto.ZhenHuanSecondProcessJoinPartyRespMsg.Builder resultMsg = respMsg.toBuilder();
        ZhenHuanSecondProcessConfig config = ZhenHuanConfigMgr.getSecondProcessConfig();
        //判断材料
        Property cost = new Property(config.getJoinGoodsId(), 1);
        //预扣除返还
        player.getModule(PreReduceModule.class).restorePreReduce(cost);

        if (respMsg.getRet() == 0) {
            //加入成功,实际扣掉
            if (!player.getModule(CurrencyModule.class).removeCurrency(cost, eLogMoneyType.ZhenHuanSecondProcess, eLogMoneyType.ZhenHuanSecondProcessJoinCost)) {
                //这里扣除失败肯定有问题
                log.error("ZhenHuanSecondProcessModule.joinPartyBack removeCurrency error, userId:{}, cost:{}", getUserId(), cost);
            }
            //加入成功立马获得茶话会等级对应的威信
            ZhenHuanLevelConfig levelConfig = config.getLevelConfig(respMsg.getDetailInfo().getLevel());
            if (levelConfig != null) {
                Property reward = new Property(config.getExchangeItemId(), levelConfig.getUseInvitationPrestige());
                player.getModule(CurrencyModule.class).addCurrency(reward, eLogMoneyType.ZhenHuanSecondProcess, eLogMoneyType.ZhenHuanSecondProcessJoinReward);
                addUserPrestige(reward);
                resultMsg.setReward(reward.toHelperString());

                AutoLogMgr.add(new LogZhenHuanSecondJoinParty(config.getActivityId(), getUserId(),
                        respMsg.getDetailInfo().getPartyId(), respMsg.getDetailInfo().getLevel(), userData.getLevel(), userData.getPrestigeDegree(), respMsg.getPos(),
                        respMsg.getTargetId(), respMsg.getTargetLevel(), respMsg.getTargetPrestige(), levelConfig.getUseInvitationPrestige()));

                UserZhenHuanSecondRelationData relationData = joinUserMap.get(respMsg.getDetailInfo().getUserId());
                if (relationData == null) {
                    synchronized (joinUserMap) {
                        relationData = joinUserMap.get(respMsg.getDetailInfo().getUserId());
                        if (relationData == null) {
                            relationData = new UserZhenHuanSecondRelationData(config.getActivityId(), getUserId(), respMsg.getDetailInfo().getUserId());
                            joinUserMap.put(respMsg.getDetailInfo().getUserId(), relationData);
                        }
                    }
                }
                relationData.setJoinAmount(relationData.getJoinAmount() + 1);
            }
        }

        player.sendPacket(ClientProtocol.U_ZHENHUAN_SECOND_PROCESS_JOIN_PARTY, resultMsg);

        syncUserData();
    }

    public int openParty(int activityId) {
        int ret = checkAction(activityId);
        if (ret != 0) {
            return ret;
        }

        ZhenHuanSecondProcessConfig config = ZhenHuanConfigMgr.getSecondProcessConfig();
        if (!config.isInDayOptionTime()) {
            return GameErrorCode.E_ACTIVITY_NOT_OPEN;
        }
        //判断材料
        Property cost = new Property(config.getPartyGoodsId(), 1);
        if (!player.getModule(CurrencyModule.class).currencyIsEnough(cost)) {
            return GameErrorCode.E_BAG_ITEM_NO_ENOUGH;
        }

        //预扣除
        player.getModule(PreReduceModule.class).preReduce(cost);

        ZhenHuanProto.CrossZhenHuanSecondProcessOpenPartyReqMsg.Builder crossReq = ZhenHuanProto.CrossZhenHuanSecondProcessOpenPartyReqMsg.newBuilder();
        crossReq.setActivityId(config.getActivityId());
        crossReq.setUserId(getUserId());
        crossReq.setUserLevel(userData.getLevel());
        crossReq.setUserBaseInfo(PlayerBasePb.parsePlayerBaseTempMsg(UserMgr.getUserBaseInfo(getUserId(), GameServer.getInstance().getServerId())));
        player.sendPacket(CrossProtocol.C_CROSS_ZHENHUAN_SECOND_PROCESS_OPEN_PARTY, crossReq);

        return 0;
    }

    public void openPartyBack(ZhenHuanProto.ZhenHuanSecondProcessOpenPartyRespMsg respMsg) {
        ZhenHuanSecondProcessConfig config = ZhenHuanConfigMgr.getSecondProcessConfig();
        //判断材料
        Property cost = new Property(config.getPartyGoodsId(), 1);
        //预扣除返还
        player.getModule(PreReduceModule.class).restorePreReduce(cost);

        if (respMsg.getRet() == 0) {
            //开启成功,实际扣掉
            if (!player.getModule(CurrencyModule.class).removeCurrency(cost, eLogMoneyType.ZhenHuanSecondProcess, eLogMoneyType.ZhenHuanSecondProcessOpenCost)) {
                //这里扣除失败肯定有问题
                log.error("ZhenHuanSecondProcessModule.openPartyBack removeCurrency error, userId:{}, cost:{}", getUserId(), cost);
            }

            Property reward = PropertyHelper.parseStringToProperty(respMsg.getReward());
            player.getModule(CurrencyModule.class).addCurrency(reward, eLogMoneyType.ZhenHuanSecondProcess, eLogMoneyType.ZhenHuanSecondProcessOpenReward);

            addUserPrestige(reward);
//            public LogZhenHuanSecondOpenParty(int activityId, long userId, long partyId, int partyLevel, long addPrestige) {
            AutoLogMgr.add(new LogZhenHuanSecondOpenParty(config.getActivityId(), getUserId(),
                    respMsg.getDetailInfo().getPartyId(), respMsg.getDetailInfo().getLevel(), reward.getCountByGoodsId(config.getExchangeItemId()).longValue()));
        }

        player.sendPacket(ClientProtocol.U_ZHENHUAN_SECOND_PROCESS_OPEN_PARTY, respMsg.toBuilder());

        syncUserData();
    }

    /**
     * 从奖励中获得威望
     *
     * @param reward
     */
    public void addUserPrestige(Property reward) {
        ZhenHuanSecondProcessConfig config = ZhenHuanConfigMgr.getSecondProcessConfig();
        if (config == null) {
            return;
        }
        BigInteger addPrestige = reward.getCountByGoodsId(config.getExchangeItemId()); //获得的威望

        userData.setPrestigeDegree(userData.getPrestigeDegree() + addPrestige.longValue());

        player.notifyListener(eGamePlayerEventType.ZhenHuanSecondProcessRank.getValue());

        syncToCross();
    }

    public void addUserPrestige(long addValue) {
        if (userData == null) {
            return;
        }
        userData.setPrestigeDegree(userData.getPrestigeDegree() + addValue);
        player.notifyListener(eGamePlayerEventType.ZhenHuanSecondProcessRank.getValue());
        syncToCross();
    }

    public int getKickRecord(int activityId) {
        int ret = checkAction(activityId);
        if (ret != 0) {
            return ret;
        }

        ZhenHuanSecondProcessConfig config = ZhenHuanConfigMgr.getSecondProcessConfig();

        ZhenHuanProto.CrossZhenHuanSecondProcessGetKickRecordListMsg.Builder crossReq = ZhenHuanProto.CrossZhenHuanSecondProcessGetKickRecordListMsg.newBuilder();
        crossReq.setActivityId(config.getActivityId());
        crossReq.setReqProtocol(ClientProtocol.U_ZHENHUAN_SECOND_PROCESS_KICK_RECORD);
        for (UserZhenHuanKickData kickData : kickDataList) {
            ZhenHuanProto.GetCrossKickRecordReqTemp.Builder temp = ZhenHuanProto.GetCrossKickRecordReqTemp.newBuilder();
            temp.setPartyId(kickData.getPartyId());
            temp.setLevel(kickData.getPartyLevel());
            temp.setOpenUserName(kickData.getOpenUserName());
            temp.setOpenUserId(kickData.getOpenUserId());
            temp.setKickUserId(kickData.getKickUserId());
            temp.setKickTime(kickData.getKickTime());

            crossReq.addReqList(temp);
        }

        player.sendPacket(CrossProtocol.C_CROSS_ZHENHUAN_SECOND_PROCESS_MY_KICK_RECORD, crossReq);

        return 0;
    }

    public void addNewKickRecord(UserZhenHuanKickData kickData) {
        synchronized (kickDataList) {
            kickDataList.add(kickData);
        }

        player.sendPacket(ClientProtocol.U_ZHENHUAN_SECOND_PROCESS_NEW_KICK_RECORD_NOTIFY,
                ZhenHuanProto.ZhenHuanSecondProcessGetNewKickRecordNotifyMsg.newBuilder().setPartyId(kickData.getPartyId()));
    }

    public int getMyPartyList(ZhenHuanProto.ZhenHuanSecondProcessMyPartyListReqMsg reqMsg) {
        int ret = checkShowTimeAction(reqMsg.getActivityId());
        if (ret != 0) {
            return ret;
        }

        ZhenHuanSecondProcessConfig config = ZhenHuanConfigMgr.getSecondProcessConfig();

        if (reqMsg.getPartyStatus() == 1) {//进行中的去跨服拉
            player.sendPacket(CrossProtocol.C_CROSS_ZHENHUAN_SECOND_PROCESS_MY_PARTY_LIST, reqMsg.toBuilder());
        } else {//已结束的在游戏服拉
            ZhenHuanProto.ZhenHuanSecondProcessMyPartyListRespMsg.Builder respMsg = ZhenHuanProto.ZhenHuanSecondProcessMyPartyListRespMsg.newBuilder();
            respMsg.setRet(0);

            List<UserZhenHuanPartyHistoryData> historyDataList = new ArrayList<>();
            synchronized (partyHistoryDataMap) {
                historyDataList.addAll(partyHistoryDataMap.values());
            }
            historyDataList.sort((o1, o2) -> {
                int compare = Long.compare(o2.getFinishTime(), o1.getFinishTime());
                if (compare == 0) {
                    compare = Long.compare(o2.getPartyId(), o1.getPartyId());
                }
                return compare;
            });

            int pageNo = reqMsg.getPage();
            int pageSize = reqMsg.getPageSize();
            int tapType = reqMsg.getTapType();
            respMsg.setPage(pageNo);
            respMsg.setPageSize(pageSize);
            respMsg.setTapType(tapType);
            respMsg.setPartyStatus(reqMsg.getPartyStatus());

            int startIndex = (pageNo - 1) * pageSize;
            int endIndex = pageNo * pageSize;
            if (startIndex >= historyDataList.size()) {
                player.sendPacket(ClientProtocol.U_ZHENHUAN_SECOND_PROCESS_MY_PARTY_LIST, respMsg);
                return 0;
            }

            int searchCount = 0;
            for (UserZhenHuanPartyHistoryData userZhenHuanPartyHistoryData : historyDataList) {
                if (tapType == 0) {//我举办的
                    if (userZhenHuanPartyHistoryData.getPos() != 1) {
                        continue;
                    }
                }

                if (tapType == 1) {//我参与的
                    if (userZhenHuanPartyHistoryData.getPos() == 1) {
                        continue;
                    }
                }

                if (searchCount < startIndex) {
                    searchCount++;
                    continue;
                }

                searchCount++;

                GameZhenHuanFinishPartyData finishPartyData = ZhenHuanMgr.getFinishPartyData(userZhenHuanPartyHistoryData.getActivityId(), userZhenHuanPartyHistoryData.getPartyId());
                if (finishPartyData == null) {
                    continue;
                }

                ZhenHuanProto.ZhenHuanSecondProcessMyPartyTemp.Builder myPartyTemp = ZhenHuanProto.ZhenHuanSecondProcessMyPartyTemp.newBuilder();
                ZhenHuanProto.ZhenHuanSecondProcessPartySimpleTemp.Builder simpleTemp = ZhenHuanProto.ZhenHuanSecondProcessPartySimpleTemp.newBuilder();
                simpleTemp.setPartyId(finishPartyData.getPartyId());
                simpleTemp.setLevel(finishPartyData.getShowPartyLevel());
                simpleTemp.setPlayerName(finishPartyData.getUserName());
                simpleTemp.setDecoration(finishPartyData.getUserDecoration());
                simpleTemp.setUserId(finishPartyData.getOpenUserId());
                simpleTemp.setIsJoin(1);
                simpleTemp.setEndTime(finishPartyData.getFinishTime());
                simpleTemp.setInNum(finishPartyData.getJoinUserList().size() - 1);
                for (ZhenHuanPartyUserSimpleInfo simpleInfo : finishPartyData.getJoinUserList()) {
                    if (simpleInfo.getUserId() == getUserId()) {
                        simpleTemp.setExtraPrestige(simpleInfo.getMatchPrestige());
                    }
                }

                simpleTemp.setOpenServerId(finishPartyData.getOpenServerId());

                myPartyTemp.setPartyInfo(simpleTemp);
                Property finishReward = PropertyHelper.parseStringToProperty(userZhenHuanPartyHistoryData.getReward());
                ZhenHuanLevelConfig levelConfig = config.getLevelConfig(finishPartyData.getPartyLevel());
                if (levelConfig != null) {
                    if (userZhenHuanPartyHistoryData.getPos() == 1) {//举办的加开宴奖励
                        finishReward.addProperty(config.getExchangeItemId(), levelConfig.getOpenPartyPrestige());
                    } else {//入座的加入座奖励
                        finishReward.addProperty(config.getExchangeItemId(), levelConfig.getUseInvitationPrestige());
                    }
                }
                myPartyTemp.setReward(finishReward.toHelperString());
                myPartyTemp.setIsGet(userZhenHuanPartyHistoryData.getGetTime() > 0);

                respMsg.addMyParty(myPartyTemp);

                if (searchCount >= endIndex) {
                    break;
                }
            }

            player.sendPacket(ClientProtocol.U_ZHENHUAN_SECOND_PROCESS_MY_PARTY_LIST, respMsg);
        }

        return 0;
    }

    public int getMyNewKickRecord(ZhenHuanProto.ZhenHuanSecondProcessGetNewKickRecordReqMsg reqMsg) {
        int ret = checkShowTimeAction(reqMsg.getActivityId());
        if (ret != 0) {
            return ret;
        }

        ZhenHuanSecondProcessConfig config = ZhenHuanConfigMgr.getSecondProcessConfig();

        long currentTime = DateHelper.getCurrentTime();

        ZhenHuanProto.CrossZhenHuanSecondProcessGetKickRecordListMsg.Builder crossReq = ZhenHuanProto.CrossZhenHuanSecondProcessGetKickRecordListMsg.newBuilder();
        crossReq.setActivityId(config.getActivityId());
        crossReq.setReqProtocol(ClientProtocol.U_ZHENHUAN_SECOND_PROCESS_GET_NEW_KICK_RECORD);


        for (UserZhenHuanKickData kickData : kickDataList) {
            if (kickData.getConfirmTime() != 0) {
                continue;
            }

            kickData.setConfirmTime(currentTime);

            ZhenHuanProto.GetCrossKickRecordReqTemp.Builder temp = ZhenHuanProto.GetCrossKickRecordReqTemp.newBuilder();
            temp.setPartyId(kickData.getPartyId());
            temp.setLevel(kickData.getPartyLevel());
            temp.setOpenUserName(kickData.getOpenUserName());
            temp.setOpenUserId(kickData.getOpenUserId());
            temp.setKickUserId(kickData.getKickUserId());
            temp.setKickTime(kickData.getKickTime());

            crossReq.addReqList(temp);
        }

        if (crossReq.getReqListCount() > 0) {
            player.sendPacket(CrossProtocol.C_CROSS_ZHENHUAN_SECOND_PROCESS_MY_KICK_RECORD, crossReq);
        } else {
            player.sendPacket(ClientProtocol.U_ZHENHUAN_SECOND_PROCESS_GET_NEW_KICK_RECORD, ZhenHuanProto.ZhenHuanSecondProcessKickRecordRespMsg.newBuilder().setRet(0));
        }

        return 0;
    }

    public List<UserZhenHuanPartyHistoryData> getCanGetRewardPartyList() {
        List<UserZhenHuanPartyHistoryData> List = new ArrayList<>();
        synchronized (partyHistoryDataMap) {
            for (UserZhenHuanPartyHistoryData historyData : partyHistoryDataMap.values()) {
                if (historyData.getGetTime() == 0) {
                    List.add(historyData);
                }
            }
        }
        return List;
    }

    public int getMyPartyReward(ZhenHuanProto.ZhenHuanSecondProcessGetPartyRewardReqMsg reqMsg) {
        int ret = checkShowTimeAction(reqMsg.getActivityId());
        if (ret != 0) {
            return ret;
        }
        ZhenHuanSecondProcessConfig secondProcessConfig = ZhenHuanConfigMgr.getSecondProcessConfig();

        ZhenHuanProto.ZhenHuanSecondProcessGetPartyRewardRespMsg.Builder respMsg = ZhenHuanProto.ZhenHuanSecondProcessGetPartyRewardRespMsg.newBuilder();
        respMsg.setRet(0);

        Property reward = new Property();
        List<UserZhenHuanPartyHistoryData> canGetRewardPartyList = getCanGetRewardPartyList();

        for (UserZhenHuanPartyHistoryData historyData : canGetRewardPartyList) {
            historyData.setGetTime(DateHelper.getCurrentTime());

            GameZhenHuanFinishPartyData finishPartyData = ZhenHuanMgr.getFinishPartyData(historyData.getActivityId(), historyData.getPartyId());
            if (finishPartyData == null) {
                continue;
            }

            reward.addProperty(PropertyHelper.parseStringToProperty(historyData.getReward()));
            ZhenHuanProto.ZhenHuanSecondPartyFinishTemp.Builder finishTemp = ZhenHuanProto.ZhenHuanSecondPartyFinishTemp.newBuilder();
            finishTemp.setPartyId(finishPartyData.getPartyId());
            finishTemp.setOpenServerId(finishPartyData.getOpenServerId());
            finishTemp.setOpenUserName(finishPartyData.getUserName());
            finishTemp.setOpenServerId(finishPartyData.getOpenServerId());
            finishTemp.setOpenDecoration(finishPartyData.getUserDecoration());
            finishTemp.setPartyLevel(finishPartyData.getPartyLevel());

            for (ZhenHuanPartyUserSimpleInfo joinUser : finishPartyData.getJoinUserList()) {
                ZhenHuanProto.ZhenHuanSecondPartyUserFinishTemp.Builder userTemp = ZhenHuanProto.ZhenHuanSecondPartyUserFinishTemp.newBuilder();
                userTemp.setUserId(joinUser.getUserId());
                userTemp.setServerId(joinUser.getServerId());
                userTemp.setUserName(joinUser.getUserName());
                userTemp.setLevel(joinUser.getLevel());
                userTemp.setDecoration(joinUser.getUserDecoration());
                userTemp.setRewardPrestige(joinUser.getMatchPrestige());
                userTemp.setPos(joinUser.getPos());
                userTemp.setIsNpc(secondProcessConfig.isNpc(joinUser.getUserId()));
                finishTemp.addUserList(userTemp);
            }

            respMsg.addFinishList(finishTemp);
        }

        if (!reward.isNothing()) {
            respMsg.setReward(reward.toHelperString());
            player.getModule(CurrencyModule.class).addCurrency(reward,
                    eLogMoneyType.ZhenHuanSecondProcess, eLogMoneyType.ZhenHuanSecondProcessPartyFinishReward);
        }

        player.sendPacket(ClientProtocol.U_ZHENHUAN_SECOND_PROCESS_GET_PARTY_REWARD, respMsg);

        removeOverNumHistoryParty();

        return 0;
    }

    public UserZhenHuanSecondProcessData getUserData() {
        return userData;
    }

    public void addPartyHistory(List<UserZhenHuanPartyHistoryData> dataList, long addPrestige) {
        synchronized (partyHistoryDataMap) {
            for (UserZhenHuanPartyHistoryData partyHistoryData : dataList) {
                partyHistoryDataMap.put(partyHistoryData.getPartyId(), partyHistoryData);
            }
        }

        for (UserZhenHuanPartyHistoryData historyData : dataList) {
            if (historyData.getPos() == 1) {//往来关系，是我加入过并结算的
                continue;
            }
            long partyId = historyData.getPartyId();
            GameZhenHuanFinishPartyData finishPartyData = ZhenHuanMgr.getFinishPartyData(historyData.getActivityId(), partyId);
            if (finishPartyData == null) {
                continue;
            }
            UserZhenHuanSecondRelationData relationData = joinUserMap.get(finishPartyData.getOpenUserId());
            if (relationData == null) {
                synchronized (joinUserMap){
                    relationData = joinUserMap.get(finishPartyData.getOpenUserId());
                    if (relationData == null) {
                        relationData = new UserZhenHuanSecondRelationData(finishPartyData.getActivityId(), getUserId(), finishPartyData.getOpenUserId());
                        joinUserMap.put(finishPartyData.getOpenUserId(), relationData);
                    }
                }
            }
            relationData.setFinishAmount(relationData.getFinishAmount() + 1);
        }

        addUserPrestige(addPrestige);

        if (player.getState() == PlayerState.ONLINE) {
            player.sendPacket(ClientProtocol.U_ZHENHUAN_SECOND_PROCESS_NEW_FINISH_PARTY_NOTIFY, ZhenHuanProto.ZhenHuanSecondProcessGetNewFinishPartyNotifyMsg.newBuilder());
            syncUserData();
        }

        for (UserZhenHuanPartyHistoryData historyData : dataList) {
            ZhenHuanLog.trackPartyFinish(player, historyData);
        }

        removeOverNumHistoryParty();
    }

    public int getRelationList(ZhenHuanProto.ZhenHuanSecondProcessRelationListReqMsg reqMsg) {
        int ret = checkAction(reqMsg.getActivityId());
        if (ret != 0) {
            return ret;
        }
        ZhenHuanSecondProcessConfig secondProcessConfig = ZhenHuanConfigMgr.getSecondProcessConfig();

        ZhenHuanProto.CrossZhenHuanSecondProcessGetUserListReqMsg.Builder crossReq = ZhenHuanProto.CrossZhenHuanSecondProcessGetUserListReqMsg.newBuilder();
        crossReq.setActivityId(secondProcessConfig.getActivityId());
        crossReq.setTapType(reqMsg.getTapType());

        //请求关系列表,把关系玩家id发到跨服组装数据
        int tapType = reqMsg.getTapType();//1 世交 2 商会 3往来
        if (tapType == 1) {
            Map<Long, RelativesInfo> relativesMap = player.getModule(RelativesModule.class).getRelativesMap();
            for (RelativesInfo relativesInfo : relativesMap.values()) {
                if (relativesInfo.getStatus() != 1) {
                    continue;
                }
                long targetUserId = 0;
                if (relativesInfo.getUserId1() == player.getUserId()) {
                    targetUserId = relativesInfo.getUserId2();
                } else {
                    targetUserId = relativesInfo.getUserId1();
                }

                crossReq.addUserIdList(targetUserId);
            }
        } else if (tapType == 2) {
            List<Long> unionMemberIdList = CrossUnionMgr.getUnionMemberIdList(player.getUserInfo().getUnionUid());
            for (Long targetUserId : unionMemberIdList) {
                if (targetUserId == getUserId()){
                    continue;
                }
                crossReq.addUserIdList(targetUserId);
            }
        } else if (tapType == 3) {
            for (UserZhenHuanSecondRelationData data : joinUserMap.values()) {
                crossReq.addUserIdList(data.getTargetUserId());
            }
        }

        player.sendPacket(CrossProtocol.C_CROSS_ZHENHUAN_SECOND_PROCESS_GET_USER_LIST, crossReq);

        return 0;
    }

    public int nextParty(ZhenHuanProto.ZhenHuanSecondProcessNextPartyReqMsg reqMsg) {
        int ret = checkAction(reqMsg.getActivityId());
        if (ret != 0) {
            return ret;
        }

        player.sendPacket(CrossProtocol.C_CROSS_ZHENHUAN_SECOND_PROCESS_NEXT_PARTY, reqMsg.toBuilder());

        return 0;
    }

    public int findUser(ZhenHuanProto.ZhenHuanSecondProcessFindUserReqMsg reqMsg) {
        int ret = checkAction(reqMsg.getActivityId());
        if (ret != 0) {
            return ret;
        }
        ZhenHuanSecondProcessConfig config = ZhenHuanConfigMgr.getSecondProcessConfig();

        if (!config.getActivityInfo().getServerIdList().contains(reqMsg.getTargetServerId())) {
            return GameErrorCode.E_ZHENHUAN_SECOND_PROCESS_FIND_SERVER_RANGE_ERR;
        }

        player.sendPacket(CrossProtocol.C_CROSS_ZHENHUAN_SECOND_PROCESS_FIND_USER, reqMsg.toBuilder());

        return 0;
    }

    //移出超出数量的茶话会历史记录
    public void removeOverNumHistoryParty() {
        ZhenHuanSecondProcessConfig secondProcessConfig = ZhenHuanConfigMgr.getSecondProcessConfig();
        if (secondProcessConfig == null) {
            return;
        }
        if (partyHistoryDataMap.size() <= secondProcessConfig.getRecordMax()) {
            return;
        }
        List<UserZhenHuanPartyHistoryData> myOpenHistoryList = new ArrayList<>();
        List<UserZhenHuanPartyHistoryData> myJoinHistoryList = new ArrayList<>();
        synchronized (partyHistoryDataMap) {
            for (UserZhenHuanPartyHistoryData partyHistoryData : partyHistoryDataMap.values()) {
                if (partyHistoryData.getGetTime() == 0) {
                    continue;
                }
                if (partyHistoryData.isDirty()) {//等待入库
                    continue;
                }
                if (partyHistoryData.getPos() == 1) {
                    myOpenHistoryList.add(partyHistoryData);
                } else {
                    myJoinHistoryList.add(partyHistoryData);
                }
            }
        }

        if (myOpenHistoryList.size() > secondProcessConfig.getRecordMax()) {
            myOpenHistoryList.sort((o1, o2) -> {
                int compare = Long.compare(o2.getFinishTime(), o1.getFinishTime());
                if (compare == 0) {
                    return Long.compare(o2.getPartyId(), o1.getPartyId());
                }
                return compare;
            });

            //需要移除的
            for (int i = secondProcessConfig.getRecordMax(); i < myOpenHistoryList.size(); i++) {
                synchronized (partyHistoryDataMap) {
                    partyHistoryDataMap.remove(myOpenHistoryList.get(i).getPartyId());
                }
            }
        }

        if (myJoinHistoryList.size() > secondProcessConfig.getRecordMax()) {
            myJoinHistoryList.sort((o1, o2) -> {
                int compare = Long.compare(o2.getFinishTime(), o1.getFinishTime());
                if (compare == 0) {
                    return Long.compare(o2.getPartyId(), o1.getPartyId());
                }
                return compare;
            });

            //需要移除的
            for (int i = secondProcessConfig.getRecordMax(); i < myJoinHistoryList.size(); i++) {
                synchronized (partyHistoryDataMap) {
                    partyHistoryDataMap.remove(myJoinHistoryList.get(i).getPartyId());
                }
            }
        }

    }
}
